import Taro from '@tarojs/taro';

import { Command } from '../constant';
import { Dispatch } from 'typings';
import { IAllReducerProps } from '../types';
import { getReducerData } from '@/redux/store';
import { extraPathsValue } from '@/redux/util';

import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },

    async page() {
      let { id, orderType } = Taro.getCurrentInstance()?.router?.params;
      let result;
      if (orderType == 1) {
        result = await api.escortTradeController.findEscorttradeDetail(id);//配送（我的发布）订单详情
      } else {
        result = await api.escortTradeController.findEscortsnatchorderDetail(id);//接单（我的接单）订单详情
      }
      if (result.code == 'K-000000') {
        action.commonChange('main.detail', result.context)
      } else {
        Taro.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    },

    async harvestCode() {//确认完成核销码校验
      let shopUser = JSON.parse(Taro.getStorageSync('b2b-wechat@login').shopUser);
      let { detail, harvestCode, orderType } = getData().main;
      const params = {
        customerId: shopUser.context.customerId,
        escortTradeId: detail.escortTradeId,//订单id
        harvestCode,//收货编码
      };
      const result = await api.escortTradeController.harvestCode(params);
      action.commonChange('main.harvestCode', '')
      if (result.code == 'K-000000') {
        Taro.showToast({
          title: '已核销',
          icon: 'none',
          duration: 2000,
        });
        action.page()
      } else {
        Taro.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    },

    async updateEscortsnatchorder() {//修改我的接单,支持上传凭证
      let shopUser = JSON.parse(Taro.getStorageSync('b2b-wechat@login').shopUser);
      let { detail, harvestCode, orderType } = getData().main;
      const result = await api.escortTradeController.updateEscortsnatchorder(detail);
      if (result.code == 'K-000000') {
        Taro.showToast({
          title: '修改成功',
          icon: 'none',
          duration: 2000,
        });
        action.page()
      } else {
        Taro.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    },

    // /escortsnatchorder/modify

    async delEscortsnatchorderDetail(id) {//删除我的接单
      const result = await api.escortTradeController.delEscortsnatchorderDetail(id);
      if (result.code == 'K-000000') {
        Taro.showToast({
          title: '删除成功',
          icon: 'none',
          duration: 2000,
        });
        Taro.navigateBack();
      } else {
        Taro.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    },

    async delEscorttradeDetail(id) {//删除我的发单
      const result = await api.escortTradeController.delEscorttradeDetail(id);
      if (result.code == 'K-000000') {
        Taro.showToast({
          title: '删除成功',
          icon: 'none',
          duration: 2000,
        });
        Taro.navigateBack();
      } else {
        Taro.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    },

    async cancelEscorttrade() {//取消订单
      let { detail, cancellationOptions, cancellationReason } = getData().main;
      let params = {
        cancellationOptions,//配送订单取消选项
        cancellationReason,//配送订单取消原因
        id: detail.id,//订单id
      }
      const result = await api.escortTradeController.cancelEscorttrade(params);
      if (result.code == 'K-000000') {
        Taro.showToast({
          title: '取消成功',
          icon: 'none',
          duration: 2000,
        });
        action.page()
      } else {
        Taro.showToast({
          title: result.message,
          icon: 'none',
          duration: 2000,
        });
      }
    }
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageOrderDetailDMain'),
  };
}
